package com.badibadi.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalBitmap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.bitmap.AbImageDownloader;
import com.ab.util.AbImageUtil;
import com.badibadi.activity.ActivityReadingActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.activity.PicturePreviewActivity;
import com.badibadi.activity.SeeOtherPeopleSpaceActivity;
import com.badibadi.activity.SelectPicts_Activity;
import com.badibadi.infos.BrowseAllPhoto_Model;
import com.badibadi.infos.ImageAttribute;
import com.badibadi.infos.Results;
import com.badibadi.infos.activity_imgModel;
import com.badibadi.infos.photoModel__;
import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.IOUtils;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.slide.MainActivity;
import com.badibadi.slide.music_select;
import com.badibadi.uniclubber.R;
import com.badibadi.waterfall.LazyScrollView;
import com.badibadi.waterfall.LazyScrollView.OnScrollListener;
import com.libjph.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
//import com.lidroid.xutils.BitmapUtils;

//这是活动中的瀑布流 AddImage
public class WaterFallFragment_Activityread extends BaseFragment {
	static WaterFallFragment_Activityread fragment, fragment2, fragment3;

	private String actId;

	private String TAG = "WaterFallFragment";
	private LazyScrollView waterfall_scroll;
	private LinearLayout waterfall_container;
	private ArrayList<LinearLayout> waterfall_items;
	private Display display;
	private DisplayImageOptions options;
	private boolean isScrollview = true;
	// private AssetManager assetManager;
	// private List<String> image_filenames;
	// private final String image_path = "images";

	// private ImageView home, camera, nav_user;

	private int itemWidth;

	private int column_count = 2;// 显示列数
	private int page_count = 12;// 每次加载12张图片
	private int page = 1;// 页码
	private int type = 1;// 1表示默认 2 表示最热 3 表示最新
	private String uid;//
	private String is_joined;
	private String activity_faqi_uid;// 活动发起者的id
	private String cid;// 俱乐部id
	// private String slide_path;
	private boolean judjud = true;

	private int current_page = 0;
	private ViewHolder holder;
	// private List<PageHomePhotoModel> homePhotoModels = null, list;
	private photoModel__ model__, slide_use;
	private List<activity_imgModel> main_activity_img;
	private RelativeLayout up_pictures_uping_img_1, Slide_Display,
			click_no_display;
	private ImageAttribute imageAttribute;
	private Results resultsss;
	private List<BrowseAllPhoto_Model> AllPhoto_Models;
	private ImageView Slide_background, Slide_start, Slide_edit;
	private View Slide_view;
	private LinearLayout linearLayout_pubuliu;
	private AlertDialog.Builder builder;
	private Dialog dialog;
	private String act_name;// 活动名字
	FinalBitmap finalBitmap;
	private AbImageDownloader mAbImageDownloader = null;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(getActivity());
				} catch (Exception e) {
					// TODO: handle exception
				}

				// 第一次加载
				// waterfall_scroll.scrollTo(1, 1);
				// homePhotoModels.addAll(list);
				try {
					//    
					if (model__.getActivity_img() != null
							&& !model__.getActivity_img().isEmpty()) {
						//    
						main_activity_img.addAll(model__.getActivity_img());
						//    
//						   ("实体类里的数据是:" + model__.toString());
						AddItemToContainer(main_activity_img, current_page,
								page_count * page);
					} else {
						Utils.showMessage(getActivity(), getResources()
								.getString(R.string.l_xa10));
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));
					if (page > 1) {
						--page;
					} else {
						page = 1;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {
					waterfall_scroll.scrollTo(1, 1);
//					   ("到顶判断");
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 4:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.wx_txt_72));
					if (page > 1) {
						--page;
					} else {
						page = 1;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 5:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 6:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.wx_txt_73));
					// ((ActivityReadingActivity)(getActivity())).LoadFragment(1);
					handler.sendEmptyMessageDelayed(9, 500);
				} catch (Exception e) {
					// TODO: handle exception
				}

				// main_activity_img.clear();
				// page = 1;
				// // Slide_GetPhoto();
				// GetPhotoData(page);
				break;
			case 7:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_xa10));// 已经没有更多咯
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 8:
				try {
					Utils.ExitPrgress(getActivity());
					// Utils.showMessage(getActivity(),
					// getResources().getString(R.string.l_xa10));
					if (judjud) {
						GetPhotoData(page);
						judjud = false;
					}
					if (slide_use.getImgPath() != null
							&& !slide_use.getImgPath().isEmpty()) {
//						   
//								+ slide_use.getImgPath().size());
						Slide_view.setVisibility(View.VISIBLE);
						// ImageLoader.getInstance().displayImage(
						// Constants.BadiDownImgUrl
						// + slide_use.getImgPath().get(0)
						// .getPath()
						// + Constants.Appactivitycover,
						// Slide_background, options);
						new BitmapHelp().displayImage(getActivity(),
								Slide_background, Constants.BadiDownImgUrl
										+ slide_use.getImgPath().get(0)
												.getPath()
										+ Constants.Appactivitycover);
						//    
						// + slide_use.getImgPath().size());
					} else {
						Slide_view.setVisibility(View.GONE);
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 9:
				try {
					((ActivityReadingActivity) (getActivity())).LoadFragment(1);
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			// case 3000:
			// try {
			// imageAttribute = (ImageAttribute) msg.getData()
			// .getSerializable("img_info");
			//    ("保存相册获取到的路径是:"
			// + imageAttribute.getService_img_address().get(0)
			// .toString());
			// if (!imageAttribute.getService_img_address().get(0)
			// .toString().equals("false")) {
			// imgInformationSave();
			// } else {
			// Utils.showMessage(getActivity(), getResources()
			// .getString(R.string.wx_txt_74));
			// }
			// } catch (Exception e) {
			// // TODO: handle exception
			// }
			//
			// break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		display = getActivity().getWindowManager().getDefaultDisplay();
		actId = getArguments().getString("actId");
		finalBitmap = FinalBitmap.create(getActivity());
		// 磁盘缓存路径
		finalBitmap.configDiskCachePath(""
				+ IOUtils.getSaveObjectPath(getActivity(), TAG));
		finalBitmap.configDiskCacheSize(10 * 1024);
		finalBitmap.configMemoryCacheSize(4);
//		   
		itemWidth = display.getWidth() / column_count;// 根据屏幕大小计算每列大小
		// assetManager = getActivity().getAssets();
		// homePhotoModels = new ArrayList<PageHomePhotoModel>();
		// Constants.FirstPageData_handler = handler;
		main_activity_img = new ArrayList<activity_imgModel>();
		holder = new ViewHolder();
		mAbImageDownloader = new AbImageDownloader(getActivity());
		mAbImageDownloader.setErrorImage(R.drawable.noimage);
		mAbImageDownloader.setLoadingImage(R.drawable.ic_stub);
		mAbImageDownloader.setType(AbImageUtil.ORIGINALIMG);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(0)).build();
		Constants.is_shuaxin = false;
		handler.sendEmptyMessage(3);

	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Constants.num_up_img_handler = handler;
		if (Constants.is_shuaxin) {
			handler.sendEmptyMessageDelayed(9, 500);
			Constants.is_shuaxin = false;
		}
	}

	private void imgInformationSave() {
		Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				//    ("提交照片的数据时实体类的数据是:" + model__.toString());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", "3");
				map.put("cid", model__.getCid());
				map.put("activity_id", actId);

				map.put("record_id", 0);
				map.put("record_article_id", 0);
				map.put("zhan_num", 0);
				try {
					map.put("img", fengzhuang_upimginfo());
				} catch (JSONException e) {
					   
				}
//				   ("上传图片发送参数是:" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "photo/imgInformationSave");
				if (content != null) {
					resultsss = Utils.checkResult_NNN(getActivity(), content);
//					   ("保存图片返回的结果是:" + content + " "
//							+ resultsss.getRetmsg());
					if (resultsss != null && !resultsss.equals("null")
							&& resultsss.isRet()) {
						handler.sendEmptyMessage(6);
					} else {
						handler.sendEmptyMessage(7);
					}
				} else {
					handler.sendEmptyMessage(5);
				}
			}
		});
	}

	private JSONObject fengzhuang_upimginfo() throws JSONException {
		JSONObject object = new JSONObject();
		String s = "";
		if (imageAttribute.getService_img_address() != null
				&& !imageAttribute.getService_img_address().isEmpty()) {
			for (int i = 0; i < imageAttribute.getService_img_address().size(); i++) {
				s += imageAttribute.getService_img_address().get(i).toString();
			}
			//    ("______上传的图片地址是:" + s);
			object.put("path", s);
		}
		object.put("photoId", model__.getPhotoId());
		object.put("uid", Utils.getUid(getActivity()));
		object.put("title", getResources().getString(R.string.wx_new_photo));
		object.put("info", imageAttribute.getInfo());
		object.put("address", imageAttribute.getAbsolute_address());
		object.put("map_x", imageAttribute.getAddress_weidu());
		object.put("map_y", imageAttribute.getAddress_jingdu());
		object.put("participator", imageAttribute.getCanyu_persons());
		String t = "";
		if (imageAttribute.getBiaoqian_map() != null
				&& !imageAttribute.getBiaoqian_map().isEmpty()) {
			//    ("数" + imageAttribute.getBiaoqian_num());
			for (int j = 0; j < imageAttribute.getBiaoqian_num(); j++) {
				if (imageAttribute.getBiaoqian_map().get(j) != null) {
					t += imageAttribute.getBiaoqian_map().get(j) + ",";
				}
			}
			object.put("typeid", quchu_zuihou(t));
		}
		object.put("attachment", imageAttribute.getLaiyuan());
		object.put("is_show", imageAttribute.getIs_displaydongtai());
		return object;
	}

	private String quchu_zuihou(String s) {
		return s.substring(0, s.length() - 1);
	}

	private void InitLayout(View view) {
		// TODO Auto-generated method stub
		waterfall_scroll = (LazyScrollView) view
				.findViewById(R.id.waterfall_scroll);
		waterfall_scroll.getView();
		waterfall_scroll.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onTop() {
				// 滚动到最顶端
				waterfall_scroll.scrollTo(1, 1);
//				   
			}

			@Override
			public void onScroll() {
				// 滚动中
//				   
			}

			@Override
			public void onBottom() {
				// 滚动到最低端
				// AddItemToContainer(++current_page, page_count);
				GetPhotoData(++page);
			}
		});

		waterfall_container = (LinearLayout) view
				.findViewById(R.id.waterfall_container);
		waterfall_container.setBackgroundColor(0xffeeeeee);

		linearLayout_pubuliu = new LinearLayout(getActivity());
		linearLayout_pubuliu.setOrientation(LinearLayout.HORIZONTAL);
		linearLayout_pubuliu.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		linearLayout_pubuliu.setBackgroundColor(0xffeeeeee);
		Slide_view.setPadding(5, 0, 5, 20);

		waterfall_items = new ArrayList<LinearLayout>();
		// 根据column_count列数弄两列linearLayout
		for (int i = 0; i < column_count; i++) {
			LinearLayout itemLayout = new LinearLayout(getActivity());
			LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(
					itemWidth, LayoutParams.WRAP_CONTENT);
			// itemParam.width = itemWidth;
			// itemParam.height = LayoutParams.WRAP_CONTENT;
			itemLayout.setPadding(5, 5, 5, 5);
			itemLayout.setOrientation(LinearLayout.VERTICAL);
			itemLayout.setLayoutParams(itemParam);
			itemLayout.setBackgroundColor(0xffeeeeee);
			waterfall_items.add(itemLayout);
			linearLayout_pubuliu.addView(itemLayout);
		}
		waterfall_container.addView(Slide_view);
		waterfall_container.addView(linearLayout_pubuliu);
		// GetPhotoData(page);
		Slide_GetPhoto();
		// 加载所有图片路径
		// try {
		// image_filenames = Arrays.asList(assetManager.list(image_path));
		//    
		// } catch (IOException e) {
		//    
		// }
		jianting();
	}

	private View Slide_View() {
		View view = null;
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		view = inflater.inflate(R.layout.main1, null);
		Slide_Display = (RelativeLayout) view.findViewById(R.id.Slide_Display);
		Slide_background = (ImageView) view.findViewById(R.id.Slide_background);
		Slide_background.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Panduan_Time()) {
					Intent intent1 = new Intent();
					intent1.setClass(getActivity(), MainActivity.class);
					intent1.putExtra("act_name", act_name);
					intent1.putExtra("slide_imgs", slide_use);
					startActivity(intent1);
				} else {
					ShowDialog();
				}
			}
		});
		Slide_start = (ImageView) view.findViewById(R.id.Slide_start);
		Slide_start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Panduan_Time()) {
					Intent intent1 = new Intent();
					intent1.setClass(getActivity(), MainActivity.class);
					intent1.putExtra("slide_imgs", slide_use);
					intent1.putExtra("act_name", act_name);
					startActivity(intent1);
				} else {
					ShowDialog();
				}
			}
		});
		Slide_edit = (ImageView) view.findViewById(R.id.Slide_edit);
		Slide_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent3 = new Intent();
				intent3.setClass(getActivity(), music_select.class);
				intent3.putExtra("actId", actId);
//				   
				startActivityForResult(intent3, 5);
			}
		});
		return view;
	}

	private void ShowDialog() {
		builder = new AlertDialog.Builder(getActivity());
		builder.setView(Return_View());
		dialog = builder.show();
	}

	private View Return_View() {
		View view = null;
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		view = inflater.inflate(R.layout.slide_no_play_tishi_main, null);
		click_no_display = (RelativeLayout) view
				.findViewById(R.id.click_no_display);
		click_no_display.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		return view;
	}

	private boolean Panduan_Time() {
		try {
			long xian_time = System.currentTimeMillis();
			long bijiao_time = Long.valueOf(slide_use.getEnd_time() + "000");
			//    
			if ((bijiao_time - xian_time) > 0) {
				return false;// 没有结束
			} else {
				return true;// 已结束
			}
		} catch (Exception e) {
			// TODO: handle exception
			return true;
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 5 && resultCode == 300) {
			String path = data.getStringExtra("slide_path");
//			   
			try {
				if (Utils.panduan_is_cunzai(path, "/Public/music/")) {
					// slide_path = path;
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.wx_txt_103));
					main_activity_img.clear();
					page = 1;
					Slide_GetPhoto();
				} else {
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.wx_txt_102));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		if (requestCode == 5 && resultCode == 5) {
			imageAttribute = (ImageAttribute) data
					.getSerializableExtra("img_info");
			imgInformationSave();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		try {
			type = getArguments().getInt("type");
			is_joined = getArguments().getString("is_joined");
			activity_faqi_uid = getArguments().getString("fq_uid");
			act_name = getArguments().getString("act_name");
			cid = getArguments().getString("cid");
		} catch (Exception e) {
			// TODO: handle exception
		}

		uid = Utils.getUid(getActivity());
//		   ("传送过来的类型是:" + type);
		View view = inflater.inflate(R.layout.waterfall, null);
		up_pictures_uping_img_1 = (RelativeLayout) view
				.findViewById(R.id.up_pictures_uping_img_1);
		up_pictures_uping_img_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(getActivity(),
				// ImgFileListActivity.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				Intent intent = new Intent(getActivity(),
						SelectPicts_Activity.class);
				intent.putExtra("panduan", "set");
				if (!StringUtil.isNullOrEmpty(cid)) {
					intent.putExtra("club", cid);
				}
				intent.putExtra("activity_id", actId);
				startActivityForResult(intent, 5);

			}
		});
		if (is_joined.equals("0")) {
			up_pictures_uping_img_1.setVisibility(View.GONE);
		} else {
			up_pictures_uping_img_1.setVisibility(View.VISIBLE);
		}
		Slide_view = Slide_View();
//		   
		if (uid.equals(activity_faqi_uid)) {
			Slide_edit.setVisibility(View.VISIBLE);
		} else {
			Slide_edit.setVisibility(View.GONE);
		}
		InitLayout(view);
		return view;
	}

	private void Slide_GetPhoto() {
		// Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				slide_use = new photoModel__();
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("actId", actId);
				String content = Utils.sendRequest(params, Constants.BadiUrl_1
						+ "activity/slideImgs");
				if (content != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							content);
					//    ("幻灯片图片浏览获取的结果是:" + content);
//					   
					if (results != null && results.getRetmsg() != null) {
						try {
							slide_use = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), photoModel__.class);
							handler.sendEmptyMessage(8);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(7);
					}
				} else {
					handler.sendEmptyMessage(5);
				}
			}
		});
	}

	private void GetPhotoData(final int page) {
		Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				model__ = new photoModel__();
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("actId", actId);
				params.put("uid", uid);
				params.put("page", page);
				params.put("pageNum", "12");
				//    ("活动阅读图片浏览传过去的参数:" + params.toString());
				String photocontent = Utils.sendRequest(params,
						Constants.BadiUrl_1 + "activity/photo");
				if (photocontent != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							photocontent);
					//    ("活动阅读图片浏览获取的结果是:" + photocontent);
					if (results != null && results.getRetmsg() != null) {
						try {
							model__ = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), photoModel__.class);
							handler.sendEmptyMessage(1);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(4);
					}
				} else {
					handler.sendEmptyMessage(2);
				}
			}
		});
	}

	private void jianting() {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				while (isScrollview) {
					try {
						if (waterfall_scroll.getScrollY() < 1) {
							handler.sendEmptyMessage(3);
						}
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						   
					}
				}
			}
		});
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		isScrollview = false;
		finalBitmap.clearCache();
		finalBitmap.clearDiskCache();
		finalBitmap.clearMemoryCache();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	private void AddItemToContainer(List<activity_imgModel> activity_img,
			int pageindex, int pagecount) {
		int j = 0;
		int imagecount = activity_img.size();
		//    ("图片有多少个:" + imagecount);
//		   
		for (int i = pageindex * pagecount; i < pagecount * (pageindex + 1)
				&& i < imagecount; i++) {
			j = (j >= column_count ? j = 0 : j);
			if (i >= page_count * (page - 1)) {
				AddImage(activity_img, j++, i);
			}
			// AddImage(main_activity_img, j++, i);
		}
		// handler.sendEmptyMessage(3);
	}

	private void AddImage(final List<activity_imgModel> list, int columnIndex,
			final int id) {
		try {
			holder.layout1 = LayoutInflater.from(getActivity());
			holder.item = holder.layout1.inflate(R.layout.waterfallitem, null)
					.findViewById(R.id.waterfallitemid);
			holder.item.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// Toast.makeText(getActivity(), "你点击了id===》" + id,
					// 0).show();

					AllPhoto_Models = new ArrayList<BrowseAllPhoto_Model>();
					BrowseAllPhoto_Model model = new BrowseAllPhoto_Model();
					model.setId(list.get(id).getId());
					model.setPath(list.get(id).getPath());
					AllPhoto_Models.add(model);
					Intent intent = new Intent(getActivity(),
							PicturePreviewActivity.class);
					intent.putExtra("picture_list",
							(Serializable) AllPhoto_Models);
					startActivity(intent);
				}
			});
			holder.zan_num = (TextView) holder.item.findViewById(R.id.zan_num);
			holder.review_num = (TextView) holder.item
					.findViewById(R.id.review_num);
			holder.collection_num = (TextView) holder.item
					.findViewById(R.id.collection_num);
			holder.childern_content = (TextView) holder.item
					.findViewById(R.id.childern_content);
			TextView how_from = (TextView) holder.item
					.findViewById(R.id.how_from);
			final ImageView waterfall_image = (ImageView) holder.item
					.findViewById(R.id.waterfall_image);
			holder.zan_num.setText(list.get(id).getView());
			holder.review_num.setText(list.get(id).getDream());
			how_from.setText(list.get(id).getNickName());
			holder.collection_num.setText(list.get(id).getCollect());
			holder.childern_content.setText(list.get(id).getTitle());
			how_from.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// Intent intent = new Intent(getActivity(),
					// SeeOtherPeopleSpaceActivity.class);
					// intent.putExtra("friend_id", list.get(id).getUserId());
					// startActivity(intent);
					if (!Utils.getUid(getActivity()).equals("")) {
						if (!list.get(id).getUserId()
								.equals("" + Utils.getUid(getActivity()))) {
							Intent intent = new Intent(getActivity(),
									SeeOtherPeopleSpaceActivity.class);
							intent.putExtra("friend_id", list.get(id)
									.getUserId());
							startActivity(intent);
						} else {
							Intent intent = new Intent(getActivity(),
									MyDataActivity.class);
							// intent.putExtra("friend_id",
							// list.get(id).getUid());
							startActivity(intent);
						}
					} else {
						Utils.showMessage(getActivity(), getResources()
								.getString(R.string.l_xb10));
					}
				}
			});
			try {

				new BitmapHelp().displayImage(getActivity(), waterfall_image,
						Constants.BadiDownImgUrl + list.get(id).getPath()
								+ Constants.appWaterfall);
				// ImageLoader.getInstance().displayImage(Constants.BadiDownImgUrl
				// + list.get(id).getPath()
				// + Constants.appWaterfall, waterfall_image, options);
				// mAbImageDownloader.display(waterfall_image,
				// Constants.BadiDownImgUrl + list.get(id).getPath()
				// + Constants.appWaterfall);
				// VolleyTools.displayImageByVolley(getActivity(),
				// Constants.BadiDownImgUrl + list.get(id).getPath(),
				// holder.waterfall_image,options);
				// ImageLoader.getInstance().loadImage(
				// Constants.BadiDownImgUrl + list.get(id).getPath()
				// + Constants.appWaterfall, options,
				// new ImageLoadingListener() {
				//
				// @Override
				// public void onLoadingStarted(String arg0, View arg1) {
				// // TODO Auto-generated method stub
				//
				// }
				//
				// @Override
				// public void onLoadingFailed(String arg0, View arg1,
				// FailReason arg2) {
				// // TODO Auto-generated method stub
				//
				// }
				//
				// @Override
				// public void onLoadingComplete(String arg0,
				// View arg1, Bitmap arg2) {
				// // TODO Auto-generated method stub
				// double width = arg2.getWidth();
				// double height = arg2.getHeight();
				// double bili = width / height;
				// double new_h = ((double) itemWidth) / bili;
				// // String
				// // path=IOUtils.getSaveObjectPath(getActivity(),
				// // MD5.getMD5(arg0));
				// // Bitmap bitmap=Utils.scaleImg(
				// // arg2, itemWidth, ((int) new_h));
				// // ImageTools.saveJPGE_After(bitmap, path);
				// // File file =new File(path);
				// // if(file.exists())
				// // {
				// // //
				// // BitmapHelp.getBitmapUtils(getActivity()).display(path,
				// // waterfall_image,
				// // BitmapHelp.getBitmapOptions(getActivity()));
				// // // BitmapUtils
				// // bmp=BitmapHelp.getBitmapUtils(getActivity());
				// // // bmp.display(waterfall_image, path,
				// // BitmapHelp.getBitmapOptions(getActivity()));
				// //
				// // finalBitmap.display(waterfall_image, path,
				// // itemWidth, ((int) new_h));
				// // }
				// waterfall_image.setImageBitmap(Utils.scaleImg(
				// arg2, itemWidth, ((int) new_h)));
				// }
				//
				// @Override
				// public void onLoadingCancelled(String arg0,
				// View arg1) {
				// // TODO Auto-generated method stub
				//
				// }
				// });
			} catch (Exception e) {

			}
			waterfall_items.get(columnIndex).addView(holder.item);
		} catch (Exception e) {
			// TODO: handle exception
			   
		}
	}

	private final class ViewHolder {
		public LayoutInflater layout1;
		public TextView zan_num,// 足迹
				review_num,// 梦想
				childern_content,// 图片名字
				how_from,// 来自哪里
				collection_num;// 收藏
		public ImageView like;
		public View item;
		public LinearLayout linearLayout1;
		// private ImageView waterfall_image;
	}

	public static WaterFallFragment_Activityread getInstance() {
		if (fragment == null) {
			return new WaterFallFragment_Activityread();
		}
		return fragment;
	}

	public static WaterFallFragment_Activityread getInstance2() {
		if (fragment2 == null) {
			return new WaterFallFragment_Activityread();
		}
		return fragment2;
	}

	public static WaterFallFragment_Activityread getInstance3() {
		if (fragment3 == null) {
			return new WaterFallFragment_Activityread();
		}
		return fragment3;
	}
}
